
#include <stdio.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <GL/glx.h>


extern GLXFBConfig select_fb(Display *dpy, Screen *scr);
extern GLXContext init_opengl(Display *dpy, Screen *scr, GLXFBConfig fbc, Window win);
extern void draw_frame(Display *dpy, Window win);
extern void finalize_opengl(Display *dpy, GLXContext);

gchar buf[260] =  { 'a', 'b', 'c', '\0' };

void
commit_callback(GtkIMContext *context,
               gchar        *str,
               gpointer      user_data)
{
    printf("%s\n", str);
}

gboolean retrieve_surrounding_callback(GtkIMContext *context, gpointer user_data)
{
    gtk_im_context_set_surrounding(context, buf, 3, 1);
    return TRUE;
}

void preedit_start_callback(GtkIMContext *context, gpointer user_data)
{

}

void preedit_changed_callback (GtkIMContext *context, gpointer user_data)
{
    static GdkRectangle area;
    area.x = 100;
    area.y = 0;
    area.width = 0;
    area.height = 30;

    gtk_im_context_set_cursor_location(context, &area);

	printf("%s\n", "dd");
	return;
}

static bool b_go = true;

void on_widget_destroyed (GtkWidget *widget, gpointer user_data)
{
	user_data = user_data;

	GdkDisplay *gdkdpy = gtk_widget_get_display (widget);
	GdkScreen *gdkscr = gtk_widget_get_screen (widget);

	Display *dpy = gdk_x11_display_get_xdisplay(gdkdpy);
	Screen *scr = gdk_x11_screen_get_xscreen(gdkscr);

	b_go = false;
}

void
on_widget_realize(GtkWidget *widget,
               gpointer   user_data)
{
    GtkIMContext *im_context = gtk_im_multicontext_new();
    GdkWindow *gdk_window = gtk_widget_get_window(widget);
    printf("set client window %x\n", gdk_window);
    gtk_im_context_set_client_window(im_context, gdk_window);

    g_signal_connect(im_context, "preedit-start", G_CALLBACK(preedit_start_callback), NULL);
    g_signal_connect(im_context, "preedit-changed", G_CALLBACK(preedit_changed_callback), NULL);
    g_signal_connect(im_context, "retrieve_surrounding", G_CALLBACK(retrieve_surrounding_callback), NULL);
    g_signal_connect(im_context, "commit", G_CALLBACK(commit_callback), NULL);


    gtk_im_context_focus_in(im_context);

    const char *p = gtk_im_multicontext_get_context_id(( GtkIMMulticontext*) im_context);
    printf("%s\n", p);
}

int main (int   argc, char *argv[])
{
    gtk_init (&argc, &argv);

    GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window), "Window");
    gtk_widget_set_size_request(window, 800, 600);

    g_signal_connect (window, "destroy", G_CALLBACK (on_widget_destroyed), NULL);
    g_signal_connect (window, "realize", G_CALLBACK (on_widget_realize), NULL);
 
    GdkDisplay *gdkdpy = gtk_widget_get_display (window);
    GdkScreen *gdkscr = gtk_widget_get_screen (window);

    Display *dpy = gdk_x11_display_get_xdisplay(gdkdpy);
    Screen *scr = gdk_x11_screen_get_xscreen(gdkscr);
    GLXFBConfig fbc = 0;

    fbc = select_fb(dpy, scr);
    XVisualInfo *vi = glXGetVisualFromFBConfig(dpy, fbc);
    printf( "Chosen visual ID = %lu\n", vi->visualid );
    GdkVisual * gdkvi =  gdk_x11_screen_lookup_visual(gdkscr, vi->visualid);

    XFree( vi );

    gtk_widget_set_visual(window, gdkvi);
    gtk_widget_show (window);

    GLXContext ctx = 0;
    GdkWindow *gdk_window = gtk_widget_get_window(window);
    ctx = init_opengl(dpy, scr, fbc, gdk_x11_window_get_xid(gdk_window));

    while (b_go)
    {
        while (gtk_events_pending())
        {
            gtk_main_iteration();
        }

        if (b_go)
            draw_frame(dpy, gdk_x11_window_get_xid(gdk_window));
    }

    finalize_opengl(dpy, ctx);

    return 0;
}

